package com.br.negocio.dao;

import java.util.Collections;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.br.negocio.vo.CarroVO;

@Service("carroDAO")
public class CarroDAO {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional(propagation = Propagation.REQUIRED)
	public void inserirCarro(CarroVO carroVO) {
		try {
			entityManager.persist(carroVO);
		} catch (Exception e) {
			throw new RuntimeException("Erro ao inserir carro: " + e.getMessage(), e);
		}
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void atualizarCarro(CarroVO carroVO) {
		try {
			entityManager.merge(carroVO);
		} catch (Exception e) {
			throw new RuntimeException("Erro ao atualizar carro: " + e.getMessage(), e);
		}
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void removerCarro(CarroVO carroVO) {
		try {
			if (!entityManager.contains(carroVO)) {
				carroVO = entityManager.merge(carroVO);
			}
			entityManager.remove(carroVO);
		} catch (Exception e) {
			throw new RuntimeException("Erro ao remover carro: " + e.getMessage(), e);
		}
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public CarroVO obterCarro(CarroVO carroVO) {
		try {
			return entityManager.find(CarroVO.class, carroVO.getCodigo());
		} catch (Exception e) {
			throw new RuntimeException("Erro ao buscar carro: " + e.getMessage(), e);
		}
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public List<CarroVO> obterListaCarro() {
		try {
			List<CarroVO> lista = (List<CarroVO>) entityManager.createNamedQuery("loadAllCarro").getResultList();
			if ((lista != null) && (!lista.isEmpty())) {
				return lista;
			} else {
				return Collections.EMPTY_LIST;
			}
		} catch (Exception e) {
			throw new RuntimeException("Erro ao carregar todos os carros.", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean validaDuplicadoPlaca(CarroVO carroVO) {
		Query query = entityManager.createNamedQuery("findDuplicatedCarroPlaca");
		query.setParameter("placa", carroVO.getPlaca());
		List<CarroVO> lista = query.getResultList();
		if (lista.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean validaDuplicadoRenavam(CarroVO carroVO) {
		Query query = entityManager.createNamedQuery("findDuplicatedCarroRenavam");
		query.setParameter("renavam", carroVO.getRenavam());
		List<CarroVO> lista = query.getResultList();
		if (lista.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}


	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
}